package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.TeamDAO;
import dao.TeamInfoDAO;
import dao.UserDAO;
import daoimpl.TeamDAOImpl;
import daoimpl.TeamInfoDAOImpl;
import daoimpl.UserDAOImpl;
import dto.TeamDTO;
import dto.TeamInfoDTO;
import dto.UserDTO;
import exceptions.DALException;

/**
 * Servlet implementation class TeamManager
 */
@WebServlet("/TeamManager")
public class TeamManager extends HttpServlet {
	private static final long serialVersionUID = 1L;


	private TeamInfoDAO teamIDAO;
	private TeamDAO teamDAO;
	private UserDTO udto;
	private int tId;
	private UserDAO userDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TeamManager() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		udto = (UserDTO) request.getSession().getAttribute("userObject");
		TeamInfoDTO tIDTO = (TeamInfoDTO) request.getSession().getAttribute("team");
		tId = tIDTO.getTeamId();
		String action = request.getParameter("action");
		int id = Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		

		try {
			teamDAO = new TeamDAOImpl();
			userDAO = new UserDAOImpl();
			teamIDAO = new TeamInfoDAOImpl();
			boolean added = false;
			switch (action) {
			case "add member":
				String userAdd = request.getParameter("name");

				for(TeamDTO team : teamDAO.getTeamPlayerList()){
					if(team.getUserId() == userDAO.getUserId(userAdd)){
						if(teamIDAO.getTeam(team.getTeamId()).getSport().equals(teamIDAO.getTeam(tIDTO.getTeamId()).getSport())){
							added=false;
						}
						else{
							added=true;
						}
					}
					else{
						added=true;
					}
				}
				if(added){
					System.out.println("Player added");
					teamDAO.createTeamPlayer(new TeamDTO(tId, userDAO.getUserId(userAdd)));
				}
				else{
					System.out.println("Player not eligable to join");
				}

				response.sendRedirect(request.getContextPath() + "/TeamSelector?id="+tId);
//				this.getServletContext().getRequestDispatcher("/WEB-INF/user/EditTeam.jsp").forward(request, response);

				break;

			case "delete member":

				String userDel = request.getParameter("name");
				if(teamIDAO.getTeam(tId).getUserId() == userDAO.getUserId(userDel)){
					System.out.println("Can't delete yourself");
				}
				else{
					teamDAO.disableTeamPlayer(new TeamDTO(tId, userDAO.getUserId(userDel)));
				}
				
				response.sendRedirect(request.getContextPath() + "/TeamSelector?id="+tId);

				break;

			case "edit team":
				
				String teamName = request.getParameter("team_name");
				String sport = request.getParameter("Sport");
				boolean canEdit = true;
	
				List<TeamDTO> teamLeadersTeam = teamDAO.getTeamList(udto.getId());
				
				for(TeamDTO leadersTeam : teamLeadersTeam){
					if(teamIDAO.getTeam(leadersTeam.getTeamId()).getSport().equals(sport)){
						canEdit = false;
					}
				}
				
				for(TeamInfoDTO teamList: teamIDAO.getTeamList()){
					if(teamList.getTeamName().equals(teamName)){
						canEdit=false;
					}
				}
				
				
				for(TeamDTO members: teamDAO.getMemberList(tId)){
					for(TeamDTO onTeam: teamDAO.getTeamList(members.getUserId())){
						if(teamIDAO.getTeam(onTeam.getTeamId()).getSport().equals(sport)&&udto.getId()!=members.getUserId()){
							teamDAO.disableTeamPlayer(new TeamDTO(tId, members.getUserId()));
						}
					}
				}
				
				if(canEdit){
				teamIDAO.updateTeam(new TeamInfoDTO(tId, teamName, sport, udto.getId()));
				}
				else{
					System.out.println("Cant edit team with this input");
				}
				response.sendRedirect(request.getContextPath() + "/TeamSelector?id="+tId);

				break;

			case "delete team":
				
				for(TeamDTO members: teamDAO.getMemberList(tId)){
				teamDAO.disableTeamPlayer(new TeamDTO(tId, members.getUserId()));
				}

				teamIDAO.disableTeam(new TeamInfoDTO(tId, null, null, 0));
				response.sendRedirect(request.getContextPath() + "/Navigation?page=Main");
				

				break;

			default:

				System.out.println("missing logic");
				response.sendRedirect(request.getContextPath() + "/TeamSelector?id="+tId);

				break;
			}
			
			teamDAO.closeConnection();
			teamIDAO.closeConnection();
			userDAO.closeConnection();
			
		
		} catch (DALException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
